import { _decorator, EventTouch, Label, Node, Sprite, SpriteFrame, UITransform, Vec3, view } from 'cc';
import { LayerType, SoundCode } from '../../Basic/Config/BaseEnum';
import { PanelBase } from '../../Basic/UI/PanelBase';
import { TowerItem } from './TowerItem';
import { AudioManager } from '../../Basic/Manager/AudioManager';
import { MainModel_Ins } from './MainModel';
import { autoComp, autoNode } from '../../Basic/Manager/BaseManager';
import { GridType } from '../../Data/Types';
import { DataMgr } from '../../Basic/Manager/DataManager';
import { EventManager } from '../../Basic/Manager/EventManager';
import { GameEventType } from '../Config/GameEnum';
import { GuideModel_Ins } from './GuideModel';
const { ccclass, property } = _decorator;
@ccclass('UpperPanel')
export class UpperPanel extends PanelBase {
  panelLayer: LayerType.DialogLayer = LayerType.DialogLayer;
  clickBackClose: boolean = true;
  blackMask: number = 0;

  @autoNode()
  upperNode: Node;
  @autoComp(Sprite)
  spr_item: Sprite;
  @autoComp(Label)
  lab_upper_cost: Label;
  @autoComp(Label)
  lab_name: Label;
  @autoComp(Label)
  lab_desc: Label;
  @autoNode()
  recoverNode: Node;
  @autoComp(Label)
  lab_recover: Label;

  @autoNode()
  btn_upper: Node;

  item: TowerItem;

  onReady(): void {
    this.refreshUI();
  }

  refreshUI() {
    this.upperNode.active = this.item.data.level < this.item.data.levelMax;
    const tbData = DataMgr.tables.TbArchitecture.get(this.item.data.id);
    if (this.upperNode.active) {
      MainModel_Ins.loadDynamicRes<SpriteFrame>(`resources/sprite/${tbData.attri[this.item.data.level - 1].img}/spriteFrame`).then((spr) => {
        this.spr_item.spriteFrame = spr;
      });
      this.lab_upper_cost.string = `${this.item.data.levelCost}`;
      this.lab_name.string = `${tbData.name}`;
      this.lab_desc.string = `威力:${this.item.data.atk}\n威慑距离:${this.item.data.atkRange}`;
    }

    this.recoverNode.active = this.item.data.type != GridType.Door;
    if (this.recoverNode.active) {
      this.lab_recover.string = `${Math.floor(this.item.data.levelCost * 0.16)}`;
    }

    if (MainModel_Ins.gameGuide === 1) {
      MainModel_Ins.gameGuide++;
      const pos = this.btn_upper.getWorldPosition();
      this.node.getComponent(UITransform).convertToNodeSpaceAR(pos, pos);
      GuideModel_Ins.Show({
        data: {
          guideNode: this.btn_upper,
          guidePos: pos,
          guideHandler: () => this.itemUpper(),
        },
      });
    }
  }

  onCloseReady(): void {}

  itemUpper() {
    if (this.item.data.level >= this.item.data.levelMax) return;
    if (this.item.IsDeah) return;
    const costCoin = Number(this.lab_upper_cost.string);
    if (MainModel_Ins.gameCoin < costCoin) return;

    MainModel_Ins.gameCoin -= costCoin;
    this.item.upper();
    MainModel_Ins.basePanel.refreshUI();
    // this.refreshUI();
    this.clickClose();
    // this.showDialog(this.selectItem);
  }

  /** 对象回收 */
  itemRecover() {
    EventManager.Ins().emit(GameEventType.MAIN_RECOVER, this.item.node);
  }

  onClickBtn(t: EventTouch, param?: any) {
    AudioManager.Singleton.playSound(SoundCode.BtnClick);
    const key = t.currentTarget.name.toString();
    switch (key) {
      case 'btn_upper':
        this.itemUpper();
        break;
      case 'btn_recover':
        this.itemRecover();
        break;
      case 'btn_close':
        this.clickClose();
        break;
    }
  }
}
